<template>
  <el-dialog
    :title="!dataForm.welderid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="焊工姓名" prop="weldername">
      <el-input v-model="dataForm.weldername" placeholder="焊工姓名"></el-input>
    </el-form-item>
    <el-form-item label="工作时长" prop="onhour">
      <el-input v-model="dataForm.onhour" placeholder="工作时长"></el-input>
    </el-form-item>
    <el-form-item label="焊接时长" prop="weldinghour">
      <el-input v-model="dataForm.weldinghour" placeholder="焊接时长"></el-input>
    </el-form-item>
    <el-form-item label="工作效率" prop="weldingradio">
      <el-input v-model="dataForm.weldingradio" placeholder="工作效率"></el-input>
    </el-form-item>
    <el-form-item label="时间" prop="weldertime">
      <el-input v-model="dataForm.weldertime" placeholder="时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          welderid: 0,
          weldername: '',
          onhour: '',
          weldinghour: '',
          weldingradio: '',
          weldertime: ''
        },
        dataRule: {
          weldername: [
            { required: true, message: '焊工姓名不能为空', trigger: 'blur' }
          ],
          onhour: [
            { required: true, message: '工作时长不能为空', trigger: 'blur' }
          ],
          weldinghour: [
            { required: true, message: '焊接时长不能为空', trigger: 'blur' }
          ],
          weldingradio: [
            { required: true, message: '工作效率不能为空', trigger: 'blur' }
          ],
          weldertime: [
            { required: true, message: '时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.welderid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.welderid) {
            this.$http({
              url: this.$http.adornUrl(`/welderradio/welderradio/info/${this.dataForm.welderid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.weldername = data.welderradio.weldername
                this.dataForm.onhour = data.welderradio.onhour
                this.dataForm.weldinghour = data.welderradio.weldinghour
                this.dataForm.weldingradio = data.welderradio.weldingradio
                this.dataForm.weldertime = data.welderradio.weldertime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/welderradio/welderradio/${!this.dataForm.welderid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'welderid': this.dataForm.welderid || undefined,
                'weldername': this.dataForm.weldername,
                'onhour': this.dataForm.onhour,
                'weldinghour': this.dataForm.weldinghour,
                'weldingradio': this.dataForm.weldingradio,
                'weldertime': this.dataForm.weldertime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
